package com.example.pda.ui.query;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.example.pda.R;
import com.example.pda.common.BaseActivity;
import com.example.pda.common.CommonCallback;
import com.example.pda.common.DialogUtils;
import com.example.pda.common.Utils;
import com.example.pda.data.model.GoodItem;
import com.example.pda.data.model.StoreBean;
import com.example.pda.databinding.ActivityQueryBinding;
import com.example.pda.databinding.ActivityQueryStoreBinding;
import com.example.pda.view.FormLayoutManger;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import per.goweii.actionbarex.common.OnActionBarChildClickListener;

public class QueryStoreActivity extends BaseActivity {
    private ActivityQueryStoreBinding binding;
    private QueryViewModel queryViewModel;
    private String mType;
    private GoodItem mCurBean;

    public static void start(Context context, String type) {
        Intent intent = new Intent(context, QueryStoreActivity.class);
        intent.putExtra("TYPE", type);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_query_store);
        binding.setLifecycleOwner(this);
        queryViewModel = new ViewModelProvider(this, new QueryViewModelFactory())
                .get(QueryViewModel.class);
        registerObserve(queryViewModel);
        binding.abc.getRightTextView().setText("查询");
        binding.abc.setOnRightTextClickListener(new OnActionBarChildClickListener() {
            @Override
            public void onClick(View v) {
                reStartActivity();
            }
        });
        mType = getIntent().getStringExtra("TYPE");

//        binding.editQuery.setText("0403100181");
        binding.btnQuery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String string = binding.editQuery.getText().toString();
                if (TextUtils.isEmpty(string)) {
                    return;
                }
                mCurBean = new GoodItem();
                mCurBean.setInvcode(string);
                queryViewModel.loadOrderQuery(mType, string);
            }
        });
        queryViewModel.mBeans.observe(this, new Observer<List<StoreBean>>() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onChanged(List<StoreBean> items) {
                adapter.getData().clear();
                if (!Utils.isEmpty(items)) {
                    adapter.setData(items);
                    List<String> titles = new ArrayList<>();
                    titles.add("");
                    adapterTitle.setData(titles);
                    mCurBean.setInvname(items.get(0).getInvname());
                } else {
                    mCurBean = new GoodItem();
                    initRecyclerView();
                }

                binding.tvInvcode.setText(String.format("存货编码: %s", mCurBean.getInvcode()));
                binding.tvInvname.setText(String.format("存货名称: %s", mCurBean.getInvname()));
            }
        });
        initRecyclerView();
    }

    private FormAdapter adapter;
    FormTitleAdapter adapterTitle;

    private void initRecyclerView() {
        binding.recyclerTitleView.setLayoutManager(new FormLayoutManger(this));
        adapterTitle = new FormTitleAdapter();
        binding.recyclerTitleView.setAdapter(adapterTitle);
        FormLayoutManger formLayoutManger = new FormLayoutManger(this, binding.recyclerView, binding.recyclerTitleView);
        binding.recyclerView.setLayoutManager(formLayoutManger);
        adapter = new FormAdapter();
        binding.recyclerView.setAdapter(adapter);

        binding.recyclerView.setNestedScrollingEnabled(false);

    }
//
//    private void addOrScan(String res) {
//        hideSoft();
//        if (!TextUtils.isEmpty(res)) {
//            String[] all = res.replaceAll("\n", "").split("丨");
//            if (all.length < 8) {
//                all = res.replaceAll("\n", "").split("\\|");
//            }
//            try {
//                if (all.length < 8) {
//                    showDialog("扫码格式错误：" + res);
//                    return;
//                }
//                // 存货编码|存货名称|规格型号|计量单位|数量|批次号|生产日期|供应商名称
//                mCurBean = new GoodItem();
//                mCurBean.setInvcode(all[0]);
//                mCurBean.setInvname(all[1]);
//                mCurBean.setGgxh(all[2]);
//                mCurBean.setSl(all[4]);
//                mCurBean.setPch(all[5]);
//                mCurBean.setGysname(all[7]);
//            } catch (Exception e) {
//                showDialog("二维码扫描解析异常，请联系管理员 e : " + e.getMessage());
//            }
//        } else {
//            showDialog("扫描数据为空");
//        }
//    }

    public void reStartActivity() {
        QueryStoreActivity.start(this, mType);
        finish();
    }
}
